package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    
    /**
     * 新增订单
     *
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 分页查询订单
     *
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> ListByPage(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id获取订单信息
     *
     * @param id
     * @return
     */
    @Select("SELECT * FROM orders WHERE id = #{id}")
    Orders getById(Long id);

    /**
     * 修改订单
     *
     * @param orders
     */
    void update(Orders orders);

    /**
     * 查询状态
     */
    @Select("select count(id) from orders where status=#{status};")
    Integer countStatus(Integer status);

    /**
     * 代替微信支付回调更新订单状态
     *
     * @param orderStatus
     * @param orderPaidStatus
     * @param checkOutTime
     * @param orderNumber
     */
    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{checkOutTime} " +
            "where number = #{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, String orderNumber);

    /**
     * 获取超时订单
     *
     * @param status
     * @param time
     */
    @Select("select * from orders where status=#{status} and order_time < #{time};")
    List<Orders> getByStatusAndTime(Integer status, LocalDateTime time);

    /**
     * 根据用户id获取订单
     * @param userId
     */
    @Select("select * from orders where user_id = #{userId} and status = #{status};")
    Orders getByUserId(Long userId,Integer status);

    /**
     * 查询当天营业额
     * @param map
     * @return
     */
//    @Select("select sum(orders.amount) where order_time &gt: #{beginTime} and order_time &lt: #{endTime}")
    Double getAmountSum(Map map);

    /**
     * 根据条件获取订单数量
     */
     Integer getOrderCountByMap(Map map);

    /**
     * 获取销量前10榜
     * @param begin
     * @param end
     * @return
     */
     List<GoodsSalesDTO> getSaleTop10(LocalDateTime begin,LocalDateTime end);
}
